package csnet.openflow.config;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import csnet.openflow.component.controller.Controller;
import csnet.openflow.component.packetGenerator.PcapGenerator;
import csnet.openflow.component.switchx.Switch;
import csnet.openflow.util.fileOperation.FileReader;
import csnet.openflow.util.log.LoggerGenerator;

public class Configuration {
    private static final String PCAP_GENERATOR = "pcap_generator";
    private static final String SWITCH = "switch";
    private static final String CONTROLLER = "controller";
    private static final String INTERVAL = "interval";
    private static final String LOGGER = "logger";

    private LoggerGenerator logger;
    private PcapGenerator generator;
    private Switch switch1;
    private Controller controller;
    private long interval;

    public Configuration(String configFile) {
        FileReader reader;
        try {
            reader = new FileReader(configFile);
            StringBuilder config = new StringBuilder();
            String current = null;
            while ((current = reader.readLine()) != null) {
                config.append(current.trim());
            }
            load(config.toString());
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void load(String config) {
        try {
            JSONObject json = new JSONObject(config);
            logger = LoggerInterpreter.interpreter(
                    json.getJSONObject(LOGGER));
            generator = PcapGeneratorInterpreter.interpreter(
                    json.getJSONObject(PCAP_GENERATOR));
            switch1 = SwitchInterpreter.interpreter(
                    json.getJSONObject(SWITCH));
            controller = ControllerInterpreter.interpreter(
                    json.getJSONObject(CONTROLLER));
            interval = json.getLong(INTERVAL);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public LoggerGenerator getLogger() {
        return logger;
    }

    public PcapGenerator getGenerator() {
        return generator;
    }

    public Switch getSwitch() {
        return switch1;
    }

    public Controller getController() {
        return controller;
    }

    public long getInterval() {
        return interval;
    }
}
